package com.yuqing.sentimentapp.home.fragment;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.base.BaseFragment;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 外媒页
 * Created by ShenHe on 2019/4/11.
 */

public class SpecialTopicFragment extends BaseFragment {
    @BindView(R.id.rb_topic)
    RadioButton mRbTopic;
    @BindView(R.id.rb_analysis)
    RadioButton mRbAnalysis;
    @BindView(R.id.group_topic)
    RadioGroup mGroupTopic;
    @BindView(R.id.layout_container)
    FrameLayout mLayoutContainer;
    private FragmentManager childFragmentManager;
    private NewsFragment topicNewsFragment;
    private NewsFragment analysisNewsFragment;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_special_topic;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        childFragmentManager = getChildFragmentManager();
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
        if (isVisible) {
            //默认显示即时专题
            setTabSelection(0);
        }
    }

    @OnClick({R.id.rb_topic, R.id.rb_analysis})
    public void onClick(View v) {
        FragmentTransaction fragmentTransaction = childFragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);
        switch (v.getId()) {
            default:
                break;
            case R.id.rb_topic:
                setTabSelection(0);
                break;
            case R.id.rb_analysis:
                setTabSelection(1);
                break;
        }
    }

    private void setTabSelection(int index) {
        FragmentTransaction ft = childFragmentManager.beginTransaction();
        hideFragment(ft);
        switch (index) {
            case 0:
                if (topicNewsFragment == null) {
                    topicNewsFragment = NewsFragment.newInstance("T");
                    ft.add(R.id.layout_container, topicNewsFragment);
                }
                ft.show(topicNewsFragment);
                break;
            case 1:
                if (analysisNewsFragment == null) {
                    analysisNewsFragment = NewsFragment.newInstance("F");
                    ft.add(R.id.layout_container, analysisNewsFragment);
                }
                ft.show(analysisNewsFragment);
                break;
        }
        ft.commit();
    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {
        if (topicNewsFragment != null) {
            fragmentTransaction.hide(topicNewsFragment);
        }
        if (analysisNewsFragment != null) {
            fragmentTransaction.hide(analysisNewsFragment);
        }
    }
}
